﻿using System;
using System.Collections.Generic;
using System.Text;
using Cosmos.Hardware;

namespace Mirus.Corelib.Text.Console
{
    /// <summary>
    /// Provides system-wide console abstraction, allowing underlaying functions to be changed at
    /// hardly any notice, with no impact on the system stability anywhere else.
    /// </summary>
    public class Console
    {
        #region Declarations
        private ConsoleColor BackgroundColor;
        private ConsoleColor ForegroundColor;
        private TextScreen Txt = Cosmos.Hardware.Global.TextScreen;
        public Utility ConsoleUtil;
        #endregion

        #region Print()
        /// <summary>
        /// Print a string to the screen
        /// </summary>
        /// <param name="s">String to print</param>
        public void Print(string s)
        {
            System.Console.Write(s);
        }

        /// <summary>
        /// Print a character to the screen
        /// </summary>
        /// <param name="c">Char to print</param>
        public void Print(char c)
        {
            System.Console.Write(c);
        }
        #endregion

        #region Read()
        /// <summary>
        /// Read the next character
        /// </summary>
        /// <param name="i">Returns int 'i'</param>
        public void Trace(out int i)
        {
            i = System.Console.Read();
        }

        /// <summary>
        /// Read the next string
        /// </summary>
        /// <param name="s">Returns string 's'</param>
        public void Trace(out string s)
        {
            s = char.ConvertFromUtf32(System.Console.Read());
        }

        /// <summary>
        /// Read the current line (as enter is pressed)
        /// </summary>
        /// <param name="s">Returns string 's'</param>
        public void ReadLine(out string s)
        {
            s = System.Console.ReadLine();
        }
        #endregion

        #region Color
        /// <summary>
        /// Change screen color
        /// </summary>
        /// <param name="color">Color to be changed to</param>
        /// <param name="index">Index bit for screen depth.  0 = BG, 1 = FG</param>
        public void ChangeColor(ConsoleColor color, int index)
        {
            // Draw to background
            if (index == 0)
            {
                BackgroundColor = color;
                UpdateColor();
            }

            // Draw to foreground
            else if (index == 1)
            {
                ForegroundColor = color;
                UpdateColor();
            }

            // Error
            else
            {

            }
        }

        /// <summary>
        /// Update any color changes
        /// </summary>
        private void UpdateColor()
        {
            System.Console.BackgroundColor = BackgroundColor;
            System.Console.ForegroundColor = ForegroundColor;
        }
        #endregion

        #region Clear()
        /// <summary>
        /// Clear the screen
        /// </summary>
        public void Clear()
        {
            //System.Console.Clear();
            Txt.Clear();
        }
        #endregion

        public Console()
        {
            BackgroundColor = ConsoleColor.Black;
            ForegroundColor = ConsoleColor.White;
            ConsoleUtil = new Utility(this);
        }
    }
}
